package com.ruoyi.shop.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.rva.framework.service.IRvaSystemService;
import com.ruoyi.rva.framework.util.RvaUtils;
import com.ruoyi.shop.domain.ShopGoods;
import com.ruoyi.shop.domain.ShopGoodsCart;
import com.ruoyi.shop.domain.ShopGoodsOrderItem;
import com.ruoyi.shop.mapper.ShopGoodsCartMapper;
import com.ruoyi.shop.mapper.ShopGoodsMapper;
import com.ruoyi.shop.mapper.ShopGoodsOrderItemMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.shop.mapper.ShopGoodsOrderMapper;
import com.ruoyi.shop.domain.ShopGoodsOrder;
import com.ruoyi.shop.service.IShopGoodsOrderService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单Service业务层处理
 * 
 * @author jiaodacailei
 * @date 2022-06-12
 */
@Service
@Transactional
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ShopGoodsOrderServiceImpl implements IShopGoodsOrderService 
{
    private final ShopGoodsOrderMapper shopGoodsOrderMapper;

    /**
     * 查询订单
     * 
     * @param id 订单主键
     * @return 订单
     */
    @Override
    public ShopGoodsOrder selectShopGoodsOrderById(Long id)
    {
        return shopGoodsOrderMapper.selectShopGoodsOrderById(id);
    }

    /**
     * 查询订单列表
     * 
     * @param shopGoodsOrder 订单
     * @return 订单
     */
    @Override
    public List<ShopGoodsOrder> selectShopGoodsOrderList(ShopGoodsOrder shopGoodsOrder)
    {
        return shopGoodsOrderMapper.selectShopGoodsOrderList(shopGoodsOrder);
    }

    /**
     * 新增订单
     * 
     * @param shopGoodsOrder 订单
     * @return 结果
     */
    @Override
    public int insertShopGoodsOrder(ShopGoodsOrder shopGoodsOrder)
    {
        shopGoodsOrder.setCreateTime(DateUtils.getNowDate());
        return shopGoodsOrderMapper.insertShopGoodsOrder(shopGoodsOrder);
    }

    /**
     * 修改订单
     * 
     * @param shopGoodsOrder 订单
     * @return 结果
     */
    @Override
    public int updateShopGoodsOrder(ShopGoodsOrder shopGoodsOrder)
    {
        shopGoodsOrder.setUpdateTime(DateUtils.getNowDate());
        return shopGoodsOrderMapper.updateShopGoodsOrder(shopGoodsOrder);
    }

    /**
     * 批量删除订单
     * 
     * @param ids 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteShopGoodsOrderByIds(Long[] ids)
    {
        return shopGoodsOrderMapper.deleteShopGoodsOrderByIds(ids);
    }

    /**
     * 删除订单信息
     * 
     * @param id 订单主键
     * @return 结果
     */
    @Override
    public int deleteShopGoodsOrderById(Long id)
    {
        return shopGoodsOrderMapper.deleteShopGoodsOrderById(id);
    }

    private final IRvaSystemService systemService;

    private final ShopGoodsCartMapper goodsCartMapper;

    private final ShopGoodsOrderItemMapper orderItemMapper;

    private final ShopGoodsMapper goodsMapper;

    @Override
    public void insertShopGoodsOrderByCart(List<Long> cartIds) {
        if (cartIds.size() > 0) {
            ShopGoodsOrder order = new ShopGoodsOrder();
            order.setCreateUpdateInfo();
            order.setStatus("01");
            List<ShopGoodsOrderItem> items = new ArrayList<>();
            String name = null;
            BigDecimal total = new BigDecimal(0.0);
            for (Long cartId : cartIds) {
                ShopGoodsCart cart = goodsCartMapper.selectShopGoodsCartById(cartId);
                ShopGoods goods = goodsMapper.selectShopGoodsById(cart.getGoodsId());
                ShopGoodsOrderItem item = new ShopGoodsOrderItem(cart, goods);
                items.add(item);
                if (name == null) {
                    name = goods.getName() + "等";
                }
                total = total.add(item.getSubTotal());
            }
            order.setName(name);
            order.setTotal(total);
            // 创建订单
            shopGoodsOrderMapper.insertShopGoodsOrder(order);
            // 关联订单并创建订单项
            items.forEach(item -> {
                item.setOrderId(order.getId());
                orderItemMapper.insertShopGoodsOrderItem(item);
            });
            // 删除购物车数据
            goodsCartMapper.deleteShopGoodsCartByIds(cartIds.toArray(new Long [0]));
        } else {
            SysUser loginUser = systemService.getLoginUser();
            List<Long> cartIdsByUser = goodsCartMapper.selectShopGoodsCartIdsByUser(loginUser.getUserId());
            if (cartIdsByUser.size() == 0) {
                RvaUtils.throwQueryException("购物车", "用户-" + loginUser.getUserName());
            }
            insertShopGoodsOrderByCart(cartIdsByUser);
        }
    }
}
